//
//  OAuthViewController.m
//  weibo110
//
//  Created by 臧松 on 15/12/26.
//  Copyright © 2015年 臧松. All rights reserved.
//

#import "OAuthViewController.h"
#import "NetworkTools.h"
#import "UserAccount.h"
#import "UserAccountViewModel.h"
#import "SVProgressHUD.h"
@interface OAuthViewController ()<UIWebViewDelegate>
@property (nonatomic,strong) UIWebView * webView;

@end

@implementation OAuthViewController
// 用户登录控制器
-(UIWebView *)webView{
    if (_webView == nil) {
        _webView = [[UIWebView alloc]init];
    }
    return _webView;
}
-(void)loadView{
    
    self.view = self.webView;
    self.webView.delegate = self;
    
    // 设置导航栏
    self.navigationItem.title = @"登录新浪微博";
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc]initWithTitle:@"关闭" style: UIBarButtonItemStylePlain target:self action:@selector(close)];
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithTitle:@"自动填充" style:UIBarButtonItemStylePlain  target:self action:@selector(autoFill)];
}
//自动填充
-(void)autoFill{
    NSString * js = @"document.getElementById('userId').value = '1771252300@qq.com';";
    
    js = [js stringByAppendingString:@"document.getElementById('passwd').value = 'zslyy15224711195'"];
    
    [self.webView stringByEvaluatingJavaScriptFromString:js];
    
}
-(void)close{
    [SVProgressHUD dismiss];
    [self dismissViewControllerAnimated:YES completion:nil];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    [self.webView loadRequest:[NSURLRequest requestWithURL:[NetworkTools sharedTools].url]];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
#pragma mark - UIWebViewDelegate

- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType{
    // 目标:如果是百度就不加载
    // 判断访问的主机是否是 www.baidu.com
    NSURL * url = request.URL;
    if (url != nil && [url.host isEqualToString:@"www.baidu.com"]) {
        
        NSString * query = url.query;
        if (query != nil && [query hasPrefix:@"code="]) {
        
            NSLog(@"%@",query);
            
            NSString * code = [query substringFromIndex:[@"code=" length]];
            NSLog(@"%@",code);
            
            [[UserAccountViewModel shareUserAccount] loadAccountTokenCode:code finished:^(BOOL isSuccessed) {
                if (!isSuccessed) {
                    [SVProgressHUD showInfoWithStatus:@"你的网路不给力"];
                    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                        
                        [self close];
                    });
                    NSLog(@"失败了");
                    return ;
                }
                    NSLog(@"成功了");
                [self dismissViewControllerAnimated:NO completion:^{
                    // 停止指示器
                    [SVProgressHUD dismiss];
                    
                    [[NSNotificationCenter defaultCenter]postNotificationName:@"WBRootViewController" object:@"welcome" userInfo:nil];
                }];
                
            }];
        
        }else{
            NSLog(@"取消授权");
            [SVProgressHUD dismiss];
            return NO;
        }
       
    }else{
        return YES;
    }
    
    return NO;
}
-(void)webViewDidStartLoad:(UIWebView *)webView{
    [SVProgressHUD show];
}
-(void)webViewDidFinishLoad:(UIWebView *)webView{
    [SVProgressHUD dismiss];
}
@end
